
<template>
  <el-form :model="inspection,car,vehicle,driver" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="车牌号" >
      <el-select v-model="inspection.carId" placeholder="请选择车牌号">
        <el-option v-for="item in car" :label="item.carNo" :value="item.id"></el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="年检标号" prop="insNo">
      <el-input v-model="inspection.insNo"></el-input>
    </el-form-item>
    <el-form-item label="年检日期" required>
      <el-col :span="11">
        <el-form-item prop="insDate">
          <el-date-picker type="date" placeholder="选择日期" v-model="inspection.insDate" style="width: 100%;"></el-date-picker>
        </el-form-item>
      </el-col>
    </el-form-item>
    <el-form-item label="下次年检" required>
      <el-col :span="11">
        <el-form-item prop="expireDate">
          <el-date-picker type="date" placeholder="选择日期" v-model="inspection.expireDate" style="width: 100%;"></el-date-picker>
        </el-form-item>
      </el-col>
    </el-form-item>
    <el-form-item label="年检金额" prop="insAmount">
      <el-input  v-model.number="inspection.insAmount"></el-input>
    </el-form-item>
    <el-form-item label="年检单位">
      <el-select v-model="inspection.vaoId" placeholder="请选择年检单位">
        <el-option v-for="item in vehicle" :label="item.unitName" :value="item.id"></el-option>
      </el-select>
      </el-form-item>

    <el-form-item label="经办人">
      <el-select v-model="inspection.operator" placeholder="请选择经办人">
        <el-option v-for="item in driver" :label="item.name" :value="item.id"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="年检备注" prop="remarks">
      <el-input v-model="inspection.remarks"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import  inspectionApi from "@/api/Inspection"

export default {
  data() {
    return {
      inspection: {
        carId: '',
        insNo: '',
        insDate: '',
        expireDate: '',
        unitName: '',
        remarks: '',
        insAmount: '',
        vaoId: '',
        operator: '',
      },
      car: {
        carNo: '',
        id: '',
      },
      vehicle: {
        id: '',
        unitName: '',
      },
      driver: {
        name: '',
        id: ''
      },

      rules: {
        carNo: [
          { required: true, message: '请输入车牌照', trigger: 'blur' },
        ],
        insNo: [
          { required: true, message: '请输入年检标号', trigger: 'blur' }

        ],
        insDate: [
          { type: 'date', required: true, message: '请选择年检日期', trigger: 'change' }
        ],
        expireDate: [
          { type: 'date', required: true, message: '请选择下次年检日期', trigger: 'change' }
        ],
        unitName: [
          { required: true, message: '请输入年检单位', trigger: 'blur' }
        ],
        remarks: [
          { required: true, message: '请输入备注', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入经办人', trigger: 'blur' }
        ],
        insAmount: [
          { required: true, message: '请输入年检金额' },
          { type: 'number', message: '金额必须是数值'}
        ],

      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert('submit!');
         this. savevInspection();
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    initInspection(){
      inspectionApi.inspectionInit().then((response)=>{
            this.driver=response.data.driver;
            this.car=response.data.car;
            this.vehicle=response.data.vehicle;
        console.log(this.driver);
        console.log(this.car);
        console.log(this.vehicle);
      })
    },
    savevInspection(){
      inspectionApi.savevInspection(this.inspection).then((response)=>{
        this.$message({
          message: "年检记录添加成功",
          type: "success",
        });
        this.$router.push({path: "/daily_work/inspection"});
      })
    }


  },created() {
    this.initInspection();
  }
}
</script>

<style>

</style>
